import axios from 'axios'
import { Command, Task } from './model'

const instance = axios.create({
  baseURL: '/v',
})

instance.interceptors.response.use(({ data }) => data)

export async function getAllCommands() {
  const list = await instance.get<void, Command[]>('/order')
  return list.map(({ id, Command_name }) => ({ id, name: Command_name }))
}

export function getAllDevices() {
  return instance.get<void, Command[]>('/device')
}

export function getAllTrainees() {
  return instance.get<void, Command[]>('/trainiee')
}

export function sendBatch(tasks: readonly Readonly<Task>[]) {
  return instance.post<void, void>('/time_command', {
    data: tasks.map(({ id, time }) => ({
      id,
      time: time.toPrimary(),
    })),
  })
}
